package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.order.OrderDetailVO;
import com.cruise.game.model.vo.order.OrderListVO;
import com.cruise.game.service.admin.AdminOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员订单控制器
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员订单相关接口")
@RestController
@RequestMapping("/api/admin/orders")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminOrderController extends BaseController {

    @Resource
    private AdminOrderService adminOrderService;

    /**
     * 获取订单列表
     */
    @ApiOperation("获取订单列表")
    @GetMapping
    public ApiResult<PageResult<OrderListVO>> getOrderList(
            @ApiParam(value = "订单状态", required = false)
            @RequestParam(value = "status", required = false) String status,

            @ApiParam(value = "搜索关键词", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,

            @ApiParam(value = "开始日期", required = false)
            @RequestParam(value = "startDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,

            @ApiParam(value = "结束日期", required = false)
            @RequestParam(value = "endDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,

            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(adminOrderService.getOrderList(status, keyword, startDate, endDate, page, pageSize));
    }

    /**
     * 获取订单详情
     */
    @ApiOperation("获取订单详情")
    @GetMapping("/{orderId}")
    public ApiResult<OrderDetailVO> getOrderDetail(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success(adminOrderService.getOrderDetail(orderId));
    }

    /**
     * 手动分配订单
     */
    @ApiOperation("手动分配订单")
    @PostMapping("/{orderId}/assign")
    public ApiResult<Boolean> assignOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId,

            @ApiParam(value = "代理ID", required = true)
            @RequestBody Map<String, Long> param) {
        Long agentId = param.get("agentId");
        if (agentId == null) {
            return ApiResult.paramError("代理ID不能为空");
        }
        return ApiResult.success("订单已分配", adminOrderService.assignOrder(orderId, agentId));
    }

    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PostMapping("/{orderId}/cancel")
    public ApiResult<Boolean> cancelOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success("订单已取消", adminOrderService.cancelOrder(orderId));
    }

    /**
     * 处理售后申请
     */
    @ApiOperation("处理售后申请")
    @PostMapping("/{orderId}/after-sales/process")
    public ApiResult<Boolean> processAfterSales(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId,

            @ApiParam(value = "是否同意", required = true)
            @RequestParam("approved") Boolean approved,

            @ApiParam(value = "处理备注", required = false)
            @RequestParam(value = "remark", required = false) String remark) {
        return ApiResult.success("售后申请已处理", adminOrderService.processAfterSales(orderId, approved, remark));
    }

    /**
     * 查看当日订单统计
     */
    @ApiOperation("查看当日订单统计")
    @GetMapping("/today-statistics")
    public ApiResult<Map<String, Object>> getTodayStatistics() {
        return ApiResult.success(adminOrderService.getTodayOrderStatistics());
    }
}